class EntriesController < ApplicationController
  before_filter [:init_custom_includes, :init_trace_nav]
  before_filter :find_entry,
    :only => [:show, :edit, :update, :destroy]
  before_filter :authenticate,
    :only => [:new, :edit, :create, :update, :destroy]
    
  def index
    @custom_stylesheets = ["entry.index",
      "jquery-ui-1.8.8.custom.css"]

    @custom_scripts = ["jquery-1.4.2", "expand", "jquery-ui-1.8.8.custom.min"]

    query_string = ""
    query_values = []
    entry_filter = []

    if params.has_key?(:filter_author)
      @filter_author = User.first(
        :conditions => ["username = ?", params[:filter_author]])

      if @filter_author != nil
        query_string << "content_generator_id = ?"
        query_values << @filter_author
      end
    end

    @filters = []

    @filters << {:name => :filter_after_date,
      :query_condition => "created_at >= ?",
      :query_condition_val => "#{params[:filter_after_date]}",
      :display_name => "Posted on or after:",
      :input_class => "select_date",
      :input_type => "text"}
    @filters << {:name => :filter_before_date,
      :query_condition => "created_at < ?",
      :query_condition_val => "#{params[:filter_before_date]}",
      :display_name => "Posted before:",
      :input_class => "select_date",
      :input_type => "text"}

    @filters.each do |filter|
      if params.has_key?(filter[:name]) and params[filter[:name]] != ""
        filter[:on] = true
        if query_string != ""
          query_string << " AND "
        end
        query_string << filter[:query_condition]
        query_values << filter[:query_condition_val]
      else
        filter[:on] = false
      end
    end

    entry_filter = [query_string]
    query_values.each { |val| entry_filter << val }

    @limit_options = [5, 10, 25, 50, 100]

    @index_limit = ((params.has_key?(:limit) and params[:limit].to_i) ?
      params[:limit].to_i : 25)

    @index_offset = ((params.has_key?(:offset) and params[:offset].to_i) ?
      params[:offset].to_i : 0)

    @non_limited_size = Entry.all(:conditions => entry_filter).size
    @entries = Entry.all(:order => "sourcedir DESC, created_at DESC",
      :conditions => entry_filter,
      :limit => @index_limit,
      :offset => @index_offset)
    
    @index_lower_limit = @index_offset + 1
    @index_upper_limit = @index_limit < @entries.size ?
      @index_offset + @index_limit : @index_offset + @entries.size

    older_offset = @index_upper_limit
    newer_offset = @index_offset - @index_limit >= 0 ?
      @index_offset - @index_limit : 0
    @show_older_href = "?limit=#{@index_limit}&amp;offset=#{older_offset}"
    @show_newer_href = "?limit=#{@index_limit}&amp;offset=#{newer_offset}"

    respond_to do |format|
      format.html
      format.xml { render :xml => @entries }
    end
  end

  def show
    @custom_stylesheets = ['entry.show', 'prettyPhoto']
    @custom_scripts = ['jquery-1.4.2', 'jquery.prettyPhoto']
    @photos = @entry.photos
    for photo in @photos
      if photo.description == nil
        photo.description = " - " 
      end
    end
    @up_one_level_label = 'Return to Entry Index'
    @up_one_level_path = entries_path
    @trace_nav << {:path => entries_path, :label => 'Entry Index'}
    respond_to do |format|
      format.html
      format.xml { render :xml => @entry }
    end
  end

  def new
    @entry = Entry.new

    respond_to do |format|
      format.html
      format.xml { render :xml => @entry }
    end
  end

	def audit
		if current_user
			sourcedirs = current_user.photoblog_sourcedirs
			unassociated_sourcedirs = Array.new
      entries_to_add = Array.new
			@added_entries = Array.new

			for new_sourcedir in sourcedirs
				if !Entry.sourcedir_is_associated?(new_sourcedir)
					entry = Entry.new
					entry.title = "#{current_user.username}:_#{new_sourcedir}"
					entry.sourcedir = new_sourcedir
					entry.content_generator = current_user
          entries_to_add << entry
				end
			end

      entries_to_add.sort! { |a,b| a.sourcedir <=> b.sourcedir }

      for entry in entries_to_add
        if entry.update_photos_and_save
          @added_entries << entry
        end
      end

			respond_to do |format|
				format.html
			end
		else

		end
	end

  def edit
    @up_one_level_path = @entry
    @up_one_level_label = 'Back to Entry'
    @trace_nav << {:path => entries_path, :label => 'Entry Index'} <<
      {:path => entry_url(@entry), :label => "&quot;#{@entry.title}&quot;"}
  end

  def create
    @entry = Entry.new(params[:entry])
    @entry.content_generator = current_user

    respond_to do |format|
      if @entry.update_photos_and_save
        flash[:notice] = 'Entry created.'
				format.html {redirect_to(@entry) }
				format.xml { render :xml => @entry, :status => :created,
	               :location => @entry }
      else
        if not current_user.photoblog_sourcedirs.include? @entry.sourcedir
          flash[:error] = 'Sourcedir does not exist!'
        end
        format.html { render :action => "new" }
				format.xml { render :xml => @entry.errors,
	               :status => :unprocessable_entity }
      end
    end
  end

  def update
	  respond_to do |format|
      if @entry.update_photos_and_attributes(params[:entry])
        flash[:notice] = 'Entry updated.'
        format.html { redirect_to(@entry) }
        format.xml { head :ok }
      else
        format.html {render :action => "edit" }
        format.xml {render :xml => @entry.errors,
                      :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @entry.destroy
    
    respond_to do |format|
      format.html { redirect_to(entries_url) }
      format.xml { head :ok }
    end
  end
  
  private
  def find_entry
    if Entry.exists?(params[:id])
      @entry = Entry.find(params[:id])
    else
      flash[:notice] = 'Entry not found.'
      respond_to do |format|
        format.html { redirect_to(entries_url) }
        format.xml { head :ok }
      end
    end	
  end
end
